<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/21 0021
 * Time: 17:07
 */
namespace Api\Controller;
use Think\Controller;

class GoodsInfoController extends Controller {

    public function goods_attribute(){
        $class_id = I('class_id');
        $where_in = "";
        if($class_id){
            $goods_class = M('goods_class')->where('class_id='.$class_id)->find();
            if($goods_class) {
                $where_in = $this->class_list($goods_class['class_id'], $where_in . $goods_class['class_id']);
                $attribute_name = M('attribute_name')->where('state=1 and class_id in (' . $where_in . ')')->order('sort desc')->select();
                $attribute_arr = array();
                $attribute_arr['class_list'] = $this->parent_class($goods_class['parent_class_id'],array('class_name'=>$goods_class['name'], 'class_id'=>$goods_class['class_id']));
                $attribute_arr['class_seo'] = array(
                    'seo_title'=>$goods_class['seo_title'],
                    'seo_keywords'=>$goods_class['seo_keywords'],
                    'seo_description'=>$goods_class['seo_description'],
                );
                foreach ($attribute_name as $k => $v) {
                    $attribute_value = M('attribute_value')->where('state=1 and attribute_name_id=' . $v['attribute_name_id'])->order('sort desc')->select();
                    $attribute_arr['attribute_list'][$k] = array(
                        'attribute_name' => $v['attribute_name'],
                        'attribute_name_id' => $v['attribute_name_id'],
                        'attribute_value' => $attribute_value
                    );
                }
                $this->ajaxReturn($attribute_arr, 'JSON');
            }
        }
    }

    private function class_list($parent_id,$where_in){
        $class_list = M('goods_class')->where('parent_class_id='.$parent_id)->select();
        if($class_list){
            foreach ($class_list as $k => $v){
                $where_in = $this->class_list($v['class_id'],$where_in.",".$v['class_id']);
            }
        }
        return $where_in;
    }

    private function parent_class($class_id,$class_str_arr){
        $class_info = M('goods_class')->where('class_id='.$class_id)->find();
        return $class_info?$this->parent_class($class_info['parent_class_id'],array('class_name' => $class_info['name'], 'class_id' => $class_info['class_id'], 'son' => $class_str_arr)):array('class_name' => '首页', 'class_id' => '0', 'son' => $class_str_arr);

    }


    public function goods_list(){
        $class_id = I('class_id');
        $order = I('order');
        $attribute = I('attribute');
        $region_id = I('region');
        $min_price = I('min_price');
        $max_price = I('max_price');

        /*分页*/
        $page = I('page')<=0?0:I('page')-1;
        $num = 12;
        $page_num = $page*$num;
        $sql_limit = " LIMIT ".$page_num.",".$num;

        /*属性*/
        $attribute_like = "";
        foreach ($attribute as $k => $v){
            $attribute_like = $attribute_like .($k==0?"":" AND "). "a.attribute like '%\"".$v."\"%'";
        }

        /*条件*/
        $where = "a.state=1 AND a.class_id=".$class_id;
        $where = $where.($min_price!=""?" AND b.price>='".$min_price."'":"");
        $where = $where.($max_price!=""?" AND b.price<='".$max_price."'":"");
        $where = $where.($region_id!=0?" AND b.region_id=".$region_id:"");
        $where = $where.($attribute_like!=""?" AND (".$attribute_like.")":"");

        switch ($order){
            case 'score_up':
                $order = " ORDER BY c.score ASC";
                break;
            case 'score_down':
                $order = " ORDER BY c.score DESC";
                break;
            case 'time_up':
                $order = " ORDER BY a.insert_time ASC";
                break;
            case 'time_down':
                $order = " ORDER BY a.insert_time DESC";
                break;
            case 'volume_up':
                $order = " ORDER BY a.volume ASC";
                break;
            case 'volume_down':
                $order = " ORDER BY a.volume DESC";
                break;
            case 'price_up':
                $order = " ORDER BY b.price ASC";
                break;
            case 'price_down':
                $order = " ORDER BY b.price DESC";
                break;
            default:
                $order = "";
                break;
        }


        $count_sql = "SELECT COUNT(*) FROM agency_goods_list AS a INNER JOIN agency_goods_price AS b ON a.goods_id=b.goods_id LEFT JOIN (SELECT goods_id,AVG(score) AS score FROM agency_goods_evaluate GROUP BY goods_id) AS c ON a.goods_id=c.goods_id WHERE ".$where.$order;
        $count = M()->query($count_sql)[0]['count(*)'];
        $all_page = ceil($count/$num);

        $list_sql = "SELECT a.goods_id,a.goods_name,a.goods_img,b.price,a.volume FROM agency_goods_list AS a INNER JOIN agency_goods_price AS b ON a.goods_id=b.goods_id LEFT JOIN (SELECT goods_id,AVG(score) AS score FROM agency_goods_evaluate GROUP BY goods_id) AS c ON a.goods_id=c.goods_id WHERE ".$where.$order.$sql_limit;
        $goods_list = M()->query($list_sql);
        //print_r($goods_list);
        $goods_arr = array();
        foreach ($goods_list as $k => $v){
            $goods_img = (array)json_decode($v['goods_img']);
            $goods_arr[]=array(
                'goods_id'=>$v['goods_id'],
                'goods_name'=>$v['goods_name'],
                'goods_img'=>$goods_img[0],
                'goods_price'=>$v['price'],
                'goods_volume'=>$v['volume']
            );
        }

        $this->ajaxReturn(array('goods_list'=>$goods_arr,'all_page'=>$all_page), 'JSON');


    }
}